#include<iostream>
using namespace std;

#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<string.h>
extern char** envrion;
const string myenv = "hello=AAAAAAAAAAAAAAAAAAAAAAAAAAA";
int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        putenv((char*)myenv.c_str());
        sleep(1);
        //child
        
        char* const argv[]={
            (char*)"other",
            nullptr
        };
        
        char* const envp[]={
            (char*)"hello execvpe1",
            (char*)"hello execvpe2",
            (char*)"hello execvpe3",
            (char*)"hello execvpe4",
        };
        printf("我是子进程,pid:%d\n",getpid());
        int i = 20;
        for(;environ[i]; i++)
        {
            printf("[%d]:%s\n",i,environ[i]);
        }
        execvpe("./other",argv,environ);//将旧的+新的环境变量再传递
        (void)envp;
        //execl("./other","other",nullptr);
        //execlp("ls","ls","-l","--color");
        //execv("/bin/ls",argv);
        exit(1);
    }
    printf("我是父进程,pid:%d\n",getpid());
    int i = 20;
    for(;environ[i]; i++)
    {
        printf("[%d]:%s\n",i,environ[i]);
    }

    pid_t rid = waitpid(id,nullptr,0);
    if(rid > 0)
    {
        cout<<"等待子进程成功"<<endl;
    }
    return 0;
}
